import * as fgui from "fairygui-cc";
import AbstractUIWindow from "../../window/AbstractUIWindow";
import UICore from "../../../../Core/Res/UICore";
import { ModelComponent } from "cc";
import { ModelGame } from "../../model/ModelGame";
import { ViewManager } from "../../base/ViewManager";
import EventMgr from "../../../../Core/Event/EventMgr";
export class UpgradeWindow extends AbstractUIWindow {
    private list: fgui.GList;
    protected getResList(): Array<string> {
        return [`Main`];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Main", "UpgradeWindow").asCom;
    }
    protected onShow(): void {
        this.list.itemRenderer = this.onlist.bind(this);
        this.list.numItems = ModelGame.pieceValues.length;
    }
    private onlist(index: number, item: fgui.GComponent): void {
        const icon = item.getChild("ui_icon") as fgui.GLoader;
        const level = item.getChild("level") as fgui.GTextField;
        const btn = item.getChild("btn") as fgui.GButton;
        icon.url = UICore.getURL("Main", ModelGame.pieceValues[index]);
        level.text = ModelGame.getlevel(ModelGame.pieceValues[index]).toString();
        btn.title = `${Number(level.text) * 100}`;
        btn.enabled = ModelGame.coin >= Number(btn.title);
        btn.grayed = ModelGame.coin < Number(btn.title);
        btn.clearClick();
        btn.onClick(() => {
            if (ModelGame.coin < Number(btn.title)) {
                ViewManager.ins.showtips("金币不足");
                return;
            }
            ViewManager.ins.showtips("升级成功");
            ModelGame.coin = -Number(btn.title);
            ModelGame.setlevel(ModelGame.pieceValues[index], Number(level.text) + 1);
            this.list.numItems = ModelGame.pieceValues.length;
            EventMgr.emit("upgrade");
        }, this);
    }
}